package com.jcw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcw.common.R;
import com.jcw.domain.Category;
import com.jcw.domain.Setmeal;
import com.jcw.domain.SetmealDish;
import com.jcw.dto.SetmealDto;
import com.jcw.service.CategoryService;
import com.jcw.service.SetmealDishService;
import com.jcw.service.SetmealService;
import com.jcw.mapper.SetmealMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author JCW
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2023-03-28 18:12:57
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private SetmealDishService setmealDishService;

    @Override
    public R<Page> pagination(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        query().like(name != null, "name", name).orderByDesc("update_time").page(pageInfo);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtil.copyProperties(pageInfo, setmealDtoPage, "records");
        //查询出所有的套餐
        List<Category> categoryList = categoryService.query().eq("type", 2).list();

        List<SetmealDto> setmealDtoList = new ArrayList<>();
        //遍历每个套餐，为它添加分类
        for (Setmeal setmeal : pageInfo.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal, setmealDto);

            for (Category category : categoryList) {
                if (category.getId().longValue() == setmeal.getCategoryId().longValue()) {
                    setmealDto.setCategoryName(category.getName());
                    break;
                }
            }
            setmealDtoList.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    @Override
    public R<String> add(SetmealDto setmealDto) {
        Setmeal setmeal = new SetmealDto();
        BeanUtil.copyProperties(setmealDto, setmeal);
        save(setmeal);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(String.valueOf(setmeal.getId())));
        setmealDishService.saveBatch(setmealDishes);
        return R.success("添加成功");
    }

    @Override
    public R<SetmealDto> echo(Long id) {
        Setmeal setmeal = getById(id);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDishService.query().eq("setmeal_id", setmealId.longValue()).list();

        SetmealDto setmealDto = new SetmealDto();
        BeanUtil.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }

    @Override
    @Transactional
    public R<String> edit(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDto, setmeal);
        update().eq("id", setmeal.getId()).update(setmeal);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        setmealDishService.removeById(setmeal.getId().longValue());
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId().toString());
            setmealDishService.update().eq("id", setmeal.getId()).update(setmealDish);
        });
        return R.success("修改成功");
    }

    @Override
    public R<String> editStatus(int statusCode, String ids) {
        String[] split = ids.split(",");
        List<Long> idList = new ArrayList();
        for (String s : split) {
            idList.add(Long.valueOf(s));
        }
        for (Long id : idList) {
            update().eq("id", id).set("status", statusCode).update();
        }

        return statusCode == 0 ? R.success("停售成功") : R.success("启售成功");
    }

    @Override
    public R<String> delete(String ids) {
        String[] split = ids.split(",");
        List<Long> idList = new ArrayList();
        for (String s : split) {
            idList.add(Long.valueOf(s));
        }
        idList.forEach(id -> removeById(id));
        return R.success("删除成功");
    }

    @Override
    public R<List> setmealList(String categoryId, Integer status) {
        List<Setmeal> setmealList = query().eq("category_id", categoryId).eq("status", status).list();
        return R.success(setmealList);
    }

}




